// pages/home/vote/vote.js
const app = getApp();
const http = require('../../../utils/http.js')
const appGlobalData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperList: null,
    //beginTime:"2021-09-30 11:18:52",
    endTime:"2021-09-30 15:42:42",
    timetext:null,
    voteList:{},
    txtString:''
  },
  timeDown:function(){
    //20天20时12分08秒
    var that=this
    var nowtime = new Date(),  //获取当前时间
        endtime = new Date(this.data.endTime);  //定义结束时间
        if(new Date(this.data.endTime)-new Date()<=0){
          that.setData({
            timetext:'活动时间已结束'
          })
          return '活动时间已结束'
        }else{
          var lefttime = endtime.getTime() - nowtime.getTime(),  //距离结束时间的毫秒数
          leftd = Math.floor(lefttime/(1000*60*60*24)),  //计算天数
          lefth = Math.floor(lefttime/(1000*60*60)%24),  //计算小时数
          leftm = Math.floor(lefttime/(1000*60)%60),  //计算分钟数
          lefts = Math.floor(lefttime/1000%60);  //计算秒数
          that.setData({
            timetext:leftd + "天" + (lefth<10?'0'+lefth:lefth) + "时" + (leftm<10?'0'+leftm:leftm) + "分" + (lefts<10?'0'+lefts:lefts)+ "秒"
          })
        }
    
  },
  tpBtn:function(e){
    var that=this
    var txtString=this.data.txtString
    console.log(e.currentTarget.dataset.id)
    if (!appGlobalData.userInfo.mobile) {//授权了，可以获取用户信息了
      //重定向到指定的页面
      wx.showModal({
        content: '请授权后再进行投票，谢谢！',
        success (res) {
          if (res.confirm) {
            //console.log('用户点击确定')
            wx.navigateTo({
              url: "/pages/auth/auth"
            });
          } else if (res.cancel) {
            //console.log('用户点击取消')
          }
        }
      })
    }else{
      wx.showModal({
        content: '确认投票吗？',
        success (res) {
          if (res.confirm) {
            //console.log('用户点击确定')
            http.get('/api/user/vote',{videoId:e.currentTarget.dataset.id}).then(res => {
              if (res.code == 200) {
                wx.showToast({
                  title:'投票成功！',
                  icon:'none'
                })
                http.get('/api/video/list',{key:txtString}).then(res => {
                  if (res.code == 200) {
                      that.setData({
                        voteList: res.data.video,
                        voteInfo: res.data,
                        endTime: res.data.endTime
                      })
                  }
                })
              }
            })
          } else if (res.cancel) {
            //console.log('用户点击取消')
          }
        }
      })
    }
  },
  ssBtn:function(){
    //console.log(this.data.txtString)
    http.get('/api/video/list',{key:this.data.txtString}).then(res => {
      if (res.code == 200) {
          this.setData({
            voteList: res.data.video,
            voteInfo: res.data,
            endTime: res.data.endTime
          })
      }
    })
  },
  inputClick:function(e){
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      txtString: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log()
    
    
    this.setData({
      customer: app.globalData.customer
    })
    http.get('/api/video/list').then(res => {
      if (res.code == 200) {
          this.setData({
              voteList: res.data.video,
              voteInfo: res.data,
              endTime: res.data.endTime
          })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this
    if(new Date(this.data.endTime)-new Date()<=0){
      that.setData({
        timetext:'活动时间已结束'
      })
    }else{
      setInterval (function () {
        that.setData({
          timetext:that.timeDown()
        })
      }, 1000);  //反复执行函数本身
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})